package com.dimong.www.loan.ui.act;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dimong.loan.sdk.ui.act.BaseFragmentActivity;
import com.dimong.loan.sdk.ui.frag.BaseFragment;
import com.dimong.loan.sdk.utils.SharedUtil;
import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.bean.UpgradeBean;
import com.dimong.www.loan.broadcastreceiver.BroadcastReceiverAction;
import com.dimong.www.loan.cache.UpgradeCache;
import com.dimong.www.loan.ui.frag.main.MainFourFrag;
import com.dimong.www.loan.ui.frag.main.MainOneFrag;
import com.dimong.www.loan.ui.frag.main.MainThreeFrag;
import com.dimong.www.loan.ui.frag.main.MainTwoFrag;
import com.dimong.www.loan.utils.ToolUtil;
import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseFragmentActivity implements OnClickListener {
    private static final int MAIN_ONE = 0;
    private static final int MAIN_TWO = 1;
    private static final int MAIN_THREE = 2;
    private static final int MAIN_FOUR = 3;

    private List<BaseFragment> listViews;
    private View ll_main_one;
    private View ll_main_two;
    private View ll_main_three;
    private View ll_main_four;

    private ImageView iv_main_one;
    private ImageView iv_main_two;
    private ImageView iv_main_three;
    private ImageView iv_main_four;

    private TextView tv_main_one;
    private TextView tv_main_two;
    private TextView tv_main_three;
    private TextView tv_main_four;

    private MainOneFrag mMainOneFrag;
    private MainTwoFrag mMainTwoFrag;
    private MainThreeFrag mMainThreeFrag;
    private MainFourFrag mMainFourFrag;
    private int index;
    private int currentTabIndex;
    private long exitTime;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BroadcastReceiverAction.REFRESH_UPGRADE.equals(intent.getAction())) {
                setBottomItem();
            }
        }
    };

    @Override
    public void handleActivityMessage(Message msg) {
        super.handleActivityMessage(msg);

    }

    @Override
    public void initIntentParam(Intent intent) {

    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public int getContentView() {
        return R.layout.act_main;
    }

    @Override
    public void initView() {
        ll_main_one = this.findViewById(R.id.ll_main_one);
        ll_main_two = this.findViewById(R.id.ll_main_two);
        ll_main_three = this.findViewById(R.id.ll_main_three);
        ll_main_four = this.findViewById(R.id.ll_main_four);

        iv_main_one = findViewById(R.id.iv_main_one);
        iv_main_two = findViewById(R.id.iv_main_two);
        iv_main_three = findViewById(R.id.iv_main_three);
        iv_main_four = findViewById(R.id.iv_main_four);

        tv_main_one = findViewById(R.id.tv_main_one);
        tv_main_two = findViewById(R.id.tv_main_two);
        tv_main_three = findViewById(R.id.tv_main_three);
        tv_main_four = findViewById(R.id.tv_main_four);
    }

    @Override
    public void setViewStatus() {
        ll_main_one.setOnClickListener(this);
        ll_main_two.setOnClickListener(this);
        ll_main_three.setOnClickListener(this);
        ll_main_four.setOnClickListener(this);

        listViews = new ArrayList<>();
        if (mMainOneFrag == null) {
            mMainOneFrag = new MainOneFrag();
        }

        if (mMainTwoFrag == null) {
            mMainTwoFrag = new MainTwoFrag();
        }
        if (mMainThreeFrag == null) {
            mMainThreeFrag = new MainThreeFrag();
        }
        if (mMainFourFrag == null) {
            mMainFourFrag = new MainFourFrag();
        }
        listViews.add(mMainOneFrag);
        listViews.add(mMainTwoFrag);
        listViews.add(mMainThreeFrag);
        listViews.add(mMainFourFrag);

        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
        trx.add(R.id.fragment_container, listViews.get(0)).commitAllowingStateLoss();
        changeImageButtonSrc(currentTabIndex);

        setBottomItem();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastReceiverAction.REFRESH_UPGRADE);
        LocalBroadcastManager.getInstance(MyApplication.getAppContext()).registerReceiver(receiver, intentFilter);
    }

    private boolean isSetBottomItem;

    private void setBottomItem() {
        if (UpgradeCache.getInstance().getUpgradeBean() != null && !isSetBottomItem) {
            isSetBottomItem = true;
            if ("1".equals(UpgradeCache.getInstance().getUpgradeBean().getIs_display_card())) {
                ll_main_two.setVisibility(View.VISIBLE);
            } else {
                ll_main_two.setVisibility(View.GONE);
            }

            if ("0".equals(UpgradeCache.getInstance().getUpgradeBean().getIs_display_loanlog())) {
                ll_main_one.setVisibility(View.GONE);
            } else {
                ll_main_one.setVisibility(View.VISIBLE);
            }

            if ("1".equals(UpgradeCache.getInstance().getUpgradeBean().getIs_update())) {
                ToolUtil.startInstallApp(this, UpgradeCache.getInstance().getUpgradeBean().getUpdate_url());
            } else if ("2".equals(UpgradeCache.getInstance().getUpgradeBean().getIs_update())) {
                ToolUtil.startInstallApp(this, UpgradeCache.getInstance().getUpgradeBean().getUpdate_url());
                finish();
            }

        } else {
            String result = SharedUtil.getPreferStr("upgradeBean");
            if (TextUtils.isEmpty(result)) {
                ll_main_two.setVisibility(View.GONE);
                ll_main_one.setVisibility(View.VISIBLE);
            } else {
                UpgradeBean upgradeBean = new Gson().fromJson(result, UpgradeBean.class);
                if ("1".equals(upgradeBean.getIs_display_card())) {
                    ll_main_two.setVisibility(View.VISIBLE);
                } else {
                    ll_main_two.setVisibility(View.GONE);
                }

                if ("0".equals(upgradeBean.getIs_display_loanlog())) {
                    ll_main_one.setVisibility(View.GONE);
                } else {
                    ll_main_one.setVisibility(View.VISIBLE);
                }
            }
        }
        ll_main_three.setVisibility(View.GONE);
    }

    @Override
    public void onActResume() {
    }

    @Override
    public void onActPause() {
    }

    @Override
    public void onActDestroy() {
        LocalBroadcastManager.getInstance(MyApplication.getAppContext()).unregisterReceiver(receiver);
    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    private void changeImageButtonSrc(int index) {
        switch (index) {
            case MAIN_ONE:
                iv_main_one.setSelected(true);
                iv_main_two.setSelected(false);
                iv_main_three.setSelected(false);
                iv_main_four.setSelected(false);

                tv_main_one.setSelected(true);
                tv_main_two.setSelected(false);
                tv_main_three.setSelected(false);
                tv_main_four.setSelected(false);
                break;
            case MAIN_TWO:
                iv_main_one.setSelected(false);
                iv_main_two.setSelected(true);
                iv_main_three.setSelected(false);
                iv_main_four.setSelected(false);

                tv_main_one.setSelected(false);
                tv_main_two.setSelected(true);
                tv_main_three.setSelected(false);
                tv_main_four.setSelected(false);
                break;
            case MAIN_THREE:
                iv_main_one.setSelected(false);
                iv_main_two.setSelected(false);
                iv_main_three.setSelected(true);
                iv_main_four.setSelected(false);

                tv_main_one.setSelected(false);
                tv_main_two.setSelected(false);
                tv_main_three.setSelected(true);
                tv_main_four.setSelected(false);
                break;
            case MAIN_FOUR:
                iv_main_one.setSelected(false);
                iv_main_two.setSelected(false);
                iv_main_three.setSelected(false);
                iv_main_four.setSelected(true);

                tv_main_one.setSelected(false);
                tv_main_two.setSelected(false);
                tv_main_three.setSelected(false);
                tv_main_four.setSelected(true);
                break;
            default:
                break;
        }
    }


    @Override
    public void onClick(View arg0) {
        int id = arg0.getId();
        switch (id) {
            case R.id.ll_main_one:
                index = 0;
                break;
            case R.id.ll_main_two:
                index = 1;
                break;
            case R.id.ll_main_three:
                index = 2;
                break;
            case R.id.ll_main_four:
                index = 3;
                break;
            default:
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(listViews.get(currentTabIndex));
            if (!listViews.get(index).isAdded()) {
                trx.add(R.id.fragment_container, listViews.get(index));
            }
            trx.show(listViews.get(index)).commitAllowingStateLoss();
            currentTabIndex = index;
            changeImageButtonSrc(currentTabIndex);
        }
    }

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - exitTime == 0 || currentTime - exitTime > 1500) {
            exitTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
        } else {
            super.onBackPressed();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }
}
